package com.yan.t3;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import javax.swing.JTextField;

public class WordThread extends Thread {
    String word;
    int startPosition = 19968; //Unicode表的19968位置至32320上的汉字
    int endPosition = 32320;
    String []words;

    private static String getWord(String str) {
        Scanner sc = new Scanner(str);
        sc.useDelimiter("[^a-z.]+");
        String word = null;
        while (sc.hasNext()) {
            try {
                word = sc.nextLine();
            } catch (Exception e) {
                String t = sc.next();
            }
        }
        return word;
    }
    JTextField showWord;
    int sleepLength = 6000;

    public void setJTextField(JTextField t) {
        showWord = t;
        showWord.setEditable(false);
    }

    public void setSleepLength(int n) {
        sleepLength = n;
    }

    public void run() {
        try {
            File fRead = new File("C:\\Users\\35754\\OneDrive\\桌面\\Code\\JavaCode\\week08\\src\\main\\resources\\word.txt");
            Reader in = new FileReader(fRead);
            BufferedReader bR = new BufferedReader(in);
            String str;
            int i=0;
            while((str = bR.readLine())!=null)
            {
                words[i++] = getWord(str);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        int k = 0;
        while (true) {
            word = words[k];
            showWord.setText(word);
            try {
                sleep(sleepLength);   //【代码1】//调用sleep方法使得线程中断sleepLength豪秒
            } catch (InterruptedException e) {
            }
            k++;
            if (k >= endPosition)
                k = startPosition;
        }
    }
}
